package com.jf.dao.impl;

import com.jf.bean.Role;
import com.jf.dao.RoleDao;
import com.jf.util.BaseDao;
import com.jf.util.MyUtils;
import com.jf.util.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao {
    @Override
    public List<Role> getAll() {
        String sql = "select * from role";
        return BaseDao.baseQuery(Role.class,sql);
    }

    @Override
    public List<Role> getPage(PageInfo pageInfo, Role role) {
        String sql = "select * from role where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(role.getRoleName())){
            sql+="and role_name = ? ";
            objs.add(role.getRoleName());
        }
        sql+="order by id desc limit ?,?";
        objs.add(pageInfo.getOffset());
        objs.add(pageInfo.getPageSize());
        return BaseDao.baseQuery(Role.class,sql,objs);
    }

    @Override
    public int getCount(Role role) {
        String sql = "select count(id) from role where 1 = 1 ";
        List<Object> objs = new ArrayList<>();
        if(MyUtils.isStringNotEmpty(role.getRoleName())){
            sql+="and role_name = ? ";
            objs.add(role.getRoleName());
        }
        return BaseDao.baseQueryCount(sql,objs);
    }

    @Override
    public int addSelect(Role role) {
        List<Object> objs = new ArrayList<>();
        String sql = "insert into role(";
        if(role.getId() != null){
            sql+="id,";
        }
        if(MyUtils.isStringNotEmpty(role.getRoleName())){
            sql+="role_name,";
        }
        if(MyUtils.isStringNotEmpty(role.getProfile())){
            sql+="profile,";
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=") values(";
        if(role.getId() != null){
            sql+="?,";
            objs.add(role.getId());
        }
        if(MyUtils.isStringNotEmpty(role.getRoleName())){
            sql+="?,";
            objs.add(role.getRoleName());
        }
        if(MyUtils.isStringNotEmpty(role.getProfile())){
            sql+="?,";
            objs.add(role.getProfile());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+=")";
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int updateSelect(Role role) {
        List<Object> objs = new ArrayList<>();
        String sql = "update role set ";
        if(MyUtils.isStringNotEmpty(role.getRoleName())){
            sql+="role_name = ?,";
            objs.add(role.getRoleName());
        }
        if(MyUtils.isStringNotEmpty(role.getProfile())){
            sql+="profile = ?, ";
            objs.add(role.getProfile());
        }
        sql=sql.substring(0,sql.lastIndexOf(","));
        sql+="where id = ?";
        objs.add(role.getId());
        return BaseDao.baseUpdate(sql,objs);
    }

    @Override
    public int batchDelete(int[] ids) {
        List<Object> objs = new ArrayList<>();
        StringBuilder sql = new StringBuilder("delete from role where id in (");
        for(int id : ids){
            sql.append("?,");
            objs.add(id);
        }
        sql=new StringBuilder(sql.substring(0,sql.lastIndexOf(",")));
        sql.append(")");
        return BaseDao.baseBatch(sql.toString(),ids.length,objs);
    }

    @Override
    public Role getById(int id) {
        return BaseDao.baseQueryBean(Role.class,"select * from role where id = ?",id);
    }
}
